package com.ou.csbs.capstone.tools;

import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Material;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import com.ou.csbs.capstone.models.RenderableModel;
import com.ou.csbs.capstone.models.shapes.Line;
import com.ou.csbs.capstone.views.worldwind.WorldWindCanvas;
/**
 * The PolyLineDrawingTool. This tool adds a new position on every mouse click (at the current mouse position) to a temporary black Line object. The Line object is finalized on a double left mouse click.
 * During finalization, a Line with all the same positions is drawn with the current attributes held by the Attribute model.
 * 
 * @author Senior Capstone
 */
public class PolyLineDrawingTool extends MouseAdapter{
	private Line tempLine;
	private ArrayList<Position> positions;
	private RenderableModel model;
	private BasicShapeAttributes creationAttributes;
	private long lastClickTime;
	private Position lastClickPosition;
	
	public PolyLineDrawingTool(RenderableModel model){
		this.model = model;
		positions = new ArrayList<Position>();
		this.creationAttributes = new BasicShapeAttributes();
		this.creationAttributes.setOutlineWidth(3);
		this.creationAttributes.setDrawInterior(false);
		this.creationAttributes.setOutlineMaterial(Material.BLACK);
		lastClickTime = System.currentTimeMillis();
	}

	@Override
	public void mousePressed(MouseEvent mouseEvent) {
		Position position = WorldWindCanvas.getWorldWindow().getCurrentPosition();
		if(mouseEvent.getButton() == MouseEvent.BUTTON1 && 
				System.currentTimeMillis() - lastClickTime <= 250 && 
				position.getLatitude().getDegrees() == lastClickPosition.getLatitude().getDegrees() &&
				position.getLongitude().getDegrees() == lastClickPosition.getLongitude().getDegrees()){
			if(tempLine != null){
				tempLine.removeShape(model);
				tempLine = null;
				//removes the next position point value (since we are ending the drawing)
				positions.remove(positions.size()-1);
				Line drawnLine = new Line(positions);
				drawnLine.drawShape(model);
				positions.clear();
			}else{
				positions.clear();
			}

		}else if(mouseEvent.getButton() == MouseEvent.BUTTON1){
			lastClickTime = System.currentTimeMillis();
			lastClickPosition = position;
			if(position != null){
				if(tempLine != null){
					positions.add(position);
					tempLine.addPosition(position);
				}else{
					positions.add(position);
					//used to hold the next position point
					positions.add(position);
					tempLine = new Line(this.creationAttributes, this.positions);
					tempLine.drawShape(this.model);
				}
			}
		} 
		mouseEvent.consume();
	}
	
	@Override
	public void mouseClicked(MouseEvent mouseEvent){
		mouseEvent.consume();
	}
	@Override
	public void mouseDragged(MouseEvent mouseEvent){
		mouseMoved(mouseEvent);
		mouseEvent.consume();
	}
	
	@Override
	public void mouseMoved(MouseEvent mouseEvent){
		Position position = WorldWindCanvas.getWorldWindow().getCurrentPosition();
		if(position != null && tempLine != null){
			//updates the next position point value
			positions.set(positions.size()-1, position);
			tempLine.updatePoint(positions.size()-1, position);
		}
	}
}
